<template>
  <div
    class="search-result-item"
    @click="handAction"
    :class="{
      active: currentSong && currentSong.id === item.id,
      playing: currentSong && currentSong.id === item.id && playing,
    }"
  >
    <div class="middle">
      <div class="top e_1">
        {{ item.name }}
        <span v-if="item.alia.length" class="alia">({{ item.alia[0] }})</span>
      </div>

      <div class="bottom e_1">
        <!-- 歌手 -->
        <span v-for="ar in item.ar" :key="ar.id">{{ ar.name }}</span>
      </div>
    </div>

    <div
      class="right"
      @click.stop="$store.commit('addToListenLaterlist', { song: item })"
    >
      <img src="../../assets/kugou/下一首播放.png" />
      <div class="playicon">
        <span></span>
        <span></span>
        <span></span>
      </div>
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";
export default {
  name: "SearchResultItem",
  props: {
    item: Object,
  },
  methods: {
    handAction() {
      // 如果点击了正在播放的就不分发
      if (this.currentSong !== this.item) {
        const action_name = "updateCurrentSongAndAddSongInPlayList";

        this.$store.dispatch(action_name, { song: this.item });
      }
    },
  },
  computed: mapState(["currentSong", "playing"]),
};
</script>

<style lang="less" scoped>
.search-result-item {
  width: 100%;
  height: 40px;
  margin-bottom: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  .middle {
    flex: 1;
    margin-left: 20px;

    .top {
      font-size: 15px;

      span {
        font-size: 14px;
      }
    }

    .bottom {
      font-size: 12px;

      span {
        &::after {
          content: "/";
        }

        &:last-of-type {
          &::after {
            content: "";
          }
        }
      }
    }
  }

  .right {
    width: 40px;
    height: 100%;
    text-align: center;
    line-height: 40px;

    img {
      width: 50%;
      vertical-align: middle;
    }
  }

  &.active {
    color: skyblue;

    .middle {
      transition: 0.2s;
      color: skyblue;
    }

    .right {
      position: relative;

      img {
        display: none;
      }

      .playicon {
        display: flex;
        justify-content: space-around;
        width: 20px;
        height: 20px;
        position: absolute;
        top: 10px;
        left: 10px;
        z-index: 1;

        span {
          width: 15%;
          height: 100%;
          background-color: skyblue;
          display: inline-block;
          animation: loading 0.8s linear infinite alternate;
          transform-origin: bottom;
          animation-play-state: paused;

          &:nth-child(1) {
            animation-delay: -0.8s;
          }

          &:nth-child(2) {
            animation-delay: 0.2s;
          }

          &:nth-child(3) {
            animation-delay: -0.4s;
          }
        }
      }
    }

    &.playing {
      .right {
        .playicon {
          span {
            animation-play-state: running;
          }
        }
      }
    }
  }
}
</style>
